/**
 * @file contests/cf1702/f/main.cpp
 * @brief
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/7/10 22:02:51
 **/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const int N = 2e5 + 10;
int a[N], b[N];
int n;
const int INF = 0x3f3f3f3f;
int main() {
  // High rating and good luck!
  int tc;
  cin >> tc;
  while (tc--) {
    for (int i = 0; i < n; ++i) cin >> a[i];
    for (int i = 0; i < n; ++i) cin >> b[i];
    unordered_multiset<int> as, bs;
    for (int i = 0; i < n; ++i) {
      while (a[i] % 2 == 0) {
        a[i] /= 2;
      }
      as.insert(a[i]);
    }
    for (int i = 0; i < n; ++i) {
      while (b[i] % 2 == 0) b[i] /= 2;
      if (as.count(b[i]) > 0) bs.insert(b[i]);
      int x = b[i];
      while (x > 1) {
        x /= 2;
        if (as.count(x) > 0) bs.insert(x);
      }
    }
    for (int i = 0; i < n; ++i) {
      int k = INF, pos = 0, x = b[i];
      x *= 2;
      while (x > 1) {
        x /= 2;
        if (as.count(x) && bs.count(x) && k > bs.count(x))
          k = bs.count(x), pos = x;
      }
      if (k != INF) {
        as.erase(pos);
        x = b[i];
        x *= 2;
        while (x > 1) {
          x /= 2;
          bs.erase(x);
        }
      }
    }
    bool ok = true;
    for (int i = 0; i < n; ++i) {
      if (as.count(a[i])) ok = false;
    }
    cout << (ok ? "YES\n" : "NO\n");
  }
  return 0;
}
